/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Base for creating a custom VGUI class; remove the functions you don't use.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

//Sort the Panel List by member.

include( "shared.lua" )
include( "admin_menu.lua" )
include( "inventory_shared.lua" )
require( "datastream" )

INV, ABT = {}
ACCStr = ""
DoAddItems = false


local PANEL = {}

local ToolImage = "VGUI/gmod_tool"
local AbilityImage = "gui/arrow"		//Custome one later
local SENTImage = "VGUI/spawnmenu/sent"		//Custom one later
local FLAGSImage = "sprites/key_14"
local StoreItemImage = ToolImage		//Default to the Tool one first.
local InvItemImage = ToolImage

local Selected = nil
local SelectedItem = ""

function PANEL:Init()
	//Create controls here
	
	local ItemList = vgui.Create( "DPanelList", self )
		ItemList:EnableHorizontal( true )
		ItemList:EnableVerticalScrollbar( true )
		ItemList:SetPadding( 5 )
		ItemList:SetSpacing( 5 )
		
	self.ItemList = ItemList
	
	RunConsoleCommand( "StreamInvContents" )
	
	
	local PointsBox = vgui.Create( "DLabel", self )
		PointsBox:SetTextColor( color_black )
		PointsBox:NoClipping( true )
		function PointsBox:Think()
			PointsBox:SetText( "Points:  " .. LocalPlayer():GetNetworkedInt( "points" ) )
		end
		
	self.PointsBox = PointsBox
		
	local StreamStatus = vgui.Create( "DLabel", self )
		StreamStatus:SetText( "Receiving Bag Contents..." )
		StreamStatus:SizeToContents()
		StreamStatus:Center()
		StreamStatus:SetVisible( true )
		
	self.StreamStatus = StreamStatus
	
	local TogBut = vgui.Create( "DButton", self )	//Let's see if we can't make the toggling faster, instead of restreaming the inventory
		TogBut:SetText( "Toggle Status" )
		function TogBut:DoClick()
			if table.HasValue( StoreAbilities, SelectedItem ) then
				if string.find( ABT[1], SelectedItem ) then
					RunConsoleCommand( "Abilities_" .. SelectedItem, "Off" )
				elseif !string.find( ABT[1], SelectedItem ) then
					RunConsoleCommand( "Abilities_" .. SelectedItem, "On" )
				end
				Selected = nil
				SelectedItem = ""
			elseif table.HasValue( StoreAccessories, SelectedItem ) then
				local itype = 0
				for k,v in pairs( AccessTypes ) do
					if string.find( SelectedItem, v ) then itype = v end
				end
				if string.find( ACCStr, SelectedItem ) then
					RunConsoleCommand( "ToggleACC", "Off", SelectedItem, itype )
				elseif !string.find( ACCStr, SelectedItem ) then
					RunConsoleCommand( "ToggleACC", "On", SelectedItem, itype )
				end
				Selected = nil
				SelectedItem = ""
			end
		end
		TogBut:SetVisible( false )
		
	self.TogBut = TogBut
	
	local SPanel = self
	local InfoBut = vgui.Create( "DButton", self )
		InfoBut:SetText( "Info" )
		function InfoBut:DoClick()
			SPanel:INFONotification()
		end
	
	self.InfoBut = InfoBut

end

function InvStreamHook( pl, handle, id, encoded, decoded )
	INV = encoded.INVTABLE
	ABT = encoded.ABTABLE
	ACCStr = encoded.AccStr
	DoAddItems = true
end
datastream.Hook( "InvStream", InvStreamHook )

function PANEL:AddItems()
	if self.ItemList then self.ItemList:Clear() end
	local InvItem = nil
	
	for k,v in pairs( INV ) do
	local ROW = string.Explode( ",", v )
		
		for rn,r in pairs( ROW ) do
			if k == 1 and r != "EMPTY" then
				self.StreamStatus:SetVisible( false )
			else
				self.StreamStatus:SetText( "Your Bag is Empty" )
			end
		
			if r != "EMPTY" then
				
				local FoundItem = false
				if !FoundItem then
					for c,d in pairs( StoreTools ) do
						if r == d then InvItemImage = ToolImage FoundItem = true break end
					end
				end
				if !FoundItem then
					for c,d in pairs( StoreAbilities ) do
						if r == d then InvItemImage = AbilityImage FoundItem = true break end
					end
				end
				if !FoundItem then
					for c,d in pairs( StoreSENTs ) do
						if r == d then InvItemImage = SENTImage FoundItem = true break end
					end
				end
				if !FoundItem then
					for c,d in pairs( StoreFLAGS ) do
						if r == d then InvItemImage = FLAGSImage FoundItem = true break end
					end
				end
				if !FoundItem then
					for c,d in pairs( StoreAccessories ) do
						if r == d then InvItemImage = "ModelPanel" FoundItem = true break end
					end
				end
		
				local InvItemPanel = vgui.Create( "DPanel" )
					InvItemPanel:SetSize( 50, 50 )
					InvItemPanel:SetTooltip( string.Replace( r, "_", " " ) )
					function InvItemPanel:Paint()
						draw.RoundedBox( 8, 0, 0, InvItemPanel:GetWide(), InvItemPanel:GetTall(), Color( 150, 150, 150, 255 ) )
					end
					
					
				if InvItemImage != "ModelPanel" then
					InvItem = vgui.Create( "DImage", InvItemPanel )
						InvItem:SetImage( InvItemImage )
						InvItem:StretchToParent( 1, 10, 1, 10 )
						InvItem.Type = "Image"	//Forogt why this is here
				else
					InvItem = vgui.Create( "SpawnIcon", InvItemPanel )
						InvItem:SetModel( StoreAccModels[r], 1 )
						InvItem:SetIconSize( 40 )
						InvItem:SetPos( 5, 5 )
						InvItem.Type = "ModelPanel"	//Forogt why this is here
				end
				
				local CursorBox = vgui.Create( "DPanel", InvItemPanel )	//Might change the paint of this to an OutlinedRect
				local Moused, Equipped = false
					CursorBox:StretchToParent( 0, 0, 0, 0 )
					function CursorBox:OnCursorEntered() Moused = true end
					function CursorBox:OnCursorExited() Moused = false end
					function CursorBox:OnMousePressed() SelectedItem = r Selected = self end
					function CursorBox:Think() if string.find( ACCStr, r ) or string.find( ABT[1], r ) then Equipped = true else Equipped = false end end
					function CursorBox:Paint()
						if Equipped then
							draw.RoundedBox( 8, 0, 0, self:GetWide(), self:GetTall(), Color( 10, 255, 10, 30 ) )
						end
						
						if Moused then
							draw.RoundedBox( 8, 0, 0, self:GetWide(), self:GetTall(), Color( 255, 255, 255, 45 ) )
						end
					end
					function CursorBox:PaintOver()
						if Selected == self then
							surface.SetDrawColor( Color( 80, 80, 255, 190 ) )
							surface.DrawOutlinedRect( 3, 3, self:GetWide() - 6, self:GetTall() - 6 )
						end
					end
					
				self.ItemList:AddItem( InvItemPanel )
			end
		end
	end
	//PrintTable( self.ItemList:GetItems() )
	print( ACCStr )
end

function PANEL:INFONotification()
	self:GetParent():SetMouseInputEnabled( false )
	self:SetMouseInputEnabled( false )

	local Message = vgui.Create( "DFrame" )
		Message:SetTitle( "Item Information" )
		Message:SetSize( 300, 150 )
		Message:Center()
		Message:ShowCloseButton( false )
		Message:MakePopup()
		Message:MoveToFront()
		
	local MessText = vgui.Create( "DLabel", Message )
		MessText:SetText( StoreItemInfoS[SelectedItem] )
		MessText:SizeToContents()
		MessText:Center()
		
	local Closebut = vgui.Create( "DButton", Message )
	local SPanel = self
		Closebut:SetText( "OK" )
		Closebut:SetSize( 60, 30 )
		Closebut:Center()
		Closebut:AlignBottom( 5 )
		Closebut.DoClick = function()
			Message:Remove()
			SPanel:GetParent():SetMouseInputEnabled( true )
			SPanel:SetMouseInputEnabled( true )
		end	

end

function PANEL:PerformLayout()
	
	// Let's make sure we perform the layout of the class this panel is based on here as well.
	self.BaseClass.PerformLayout( self )
	
	self.ItemList:SetPos( self:GetWide() * 0.02, self:GetTall() * 0.02 )
	self.ItemList:SetSize( self:GetWide() * 0.96, self:GetTall() * 0.78	)
	
	self.TogBut:SetSize( 100, 30 )	//Work on setting these visible or not visible.
	self.TogBut:Center()
	self.TogBut:AlignBottom( 10 )
	
	self.InfoBut:SetSize( 100, 30 )
	self.InfoBut:AlignBottom( 10 )
	self.InfoBut:MoveRightOf( self.TogBut, 5 )
	
	self.PointsBox:SetWide( 85 )
	self.PointsBox:AlignBottom( 12 )
	self.PointsBox:MoveRightOf( self.InfoBut, 20 )
	
	self.StreamStatus:SizeToContents()
	self.StreamStatus:Center()
	
	//Panel positioning here

end

function PANEL:Paint()
	//Do Painting here
	
	self.BaseClass.Paint( self )
	
	function self.PointsBox:Paint()
		surface.SetDrawColor( Color( 255, 255, 255, 255 ) )
		surface.DrawRect( -5, -2, self:GetWide(), self:GetTall() + 2 )
		
		surface.SetDrawColor( Color( 0, 0, 0, 255 ) )
		surface.DrawOutlinedRect( -5, -2, self:GetWide(), self:GetTall() + 2 )
	end

end

function PANEL:PaintOver()

	//Paint over self and child elements here.

end

function PANEL:Think()
	//Think stuff here
	
	if (table.HasValue( StoreAbilities, SelectedItem ) or table.HasValue( StoreAccessories, SelectedItem )) and !self.TogBut:IsVisible() then
		self.TogBut:SetVisible( true )
	elseif !(table.HasValue( StoreAbilities, SelectedItem ) or table.HasValue( StoreAccessories, SelectedItem )) and self.TogBut:IsVisible() then
		self.TogBut:SetVisible( false )
	end
	
	if Selected != nil and !self.InfoBut:IsVisible() then
		self.InfoBut:SetVisible( true )
	elseif Selected == nil and self.InfoBut:IsVisible() then
		self.InfoBut:SetVisible( false )
	end
	
	if DoAddItems then
		self:AddItems()
		DoAddItems = false
	end

end

function PANEL:OnCursorEntered()

	//Do stuff when the cursor enters the panel

end

function PANEL:OnCursorExited()

	//Do Stuff when the cursor leaves the panel

end

function PANEL:OnMouseWheeled()

	//Do stuff when you wheel the mouse on this panel

end

function PANEL:OnMousePressed()

	//Do stuff when you press the mouse on this panel
	Selected = nil
	SelectedItem = ""

end

function PANEL:OnMouseReleased()

	//Do stuff when you release the mouse on this panel

end

function PANEL:OnKeyCodePressed()

	//Do stuff when using keys from the keyboard on this panel. This hook uses the keycode enumerations
	//http://wiki.garrysmod.com/?title=P.OnKeyCodePressed	For more information

end

function PANEL:ActionSignal()

	//This is called on the parent frame when a child VGUI element is used. The key corresponds to what is set by Panel.SetCommand.
	//From the wiki
	
end

function PANEL:OnCursorMoved()

	//From the wiki
	//Called whenever the the mouse is moved within the panel.

end

function PANEL:DoClick()

	//For button type panels, use this. Called when you click the button.

end


//Register syntax - Desired element name, Panel to use, base class of the new element.
vgui.Register( "InvBag", PANEL, "DPanel" )